/* -+- file  ./kf2/Roster/GtkChatView.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


using System;
using Habazie.kf2.Core;

namespace Habazie.kf2.GUI
{
	
	/// <summary>
	/// 	Gtk-Based chat view widget
	/// </summary>
	public class GtkChatView : Gtk.ScrolledWindow, IChatView
	{
		private Gtk.TextView	m_textview;
		private Gtk.TextBuffer	m_buffer;
		
		private static Gtk.TextTag		m_tagin;
		private static Gtk.TextTag		m_tagtime;
		private static Gtk.TextTag		m_tagout;
		private static Gtk.TextTag		m_tagevent;
		private static Gtk.TextTagTable	m_table;
		
		static GtkChatView ()
		{
			// setup tags
			m_table = new Gtk.TextTagTable ();

			m_tagin = new Gtk.TextTag ("in");
			m_tagin.Foreground = Util.Config.GetString ("color-in");
			
			m_tagout = new Gtk.TextTag ("out");
			m_tagout.Foreground = Util.Config.GetString ("color-out");
			
			m_tagtime = new Gtk.TextTag ("time");
			m_tagtime.Foreground = Util.Config.GetString ("color-time");
			
			m_tagevent = new Gtk.TextTag ("event");
			m_tagevent.Foreground = Util.Config.GetString ("color-event");
			
			m_table.Add (m_tagin);
			m_table.Add (m_tagout);
			m_table.Add (m_tagtime);
			m_table.Add (m_tagevent);
		}
		
		public GtkChatView() : base ()
		{
			this.ShadowType = Gtk.ShadowType.In;
			this.HscrollbarPolicy = Gtk.PolicyType.Automatic;
			this.VscrollbarPolicy = Gtk.PolicyType.Automatic;
			
			m_buffer = new Gtk.TextBuffer (m_table);
			m_textview = new Gtk.TextView (m_buffer);
			m_textview.Editable = false;
			m_textview.CursorVisible = false;
			
			m_textview.WrapMode = Gtk.WrapMode.Word;
			
			this.Add (m_textview);
		}
		
		public bool CanReparent {
			get { return true; }
		}

		///<summary>Get a Gtk.Widget</summary>
		public Gtk.Widget GetWidget ()
		{
			return this;
		}
		
		//static explicit operator int (IChatView view);
		public void Clear ()
		{
			m_buffer.Clear ();
		}
		
		/// <summary>
		/// 	Display a message in this view
		/// </summary>
		public void DisplayMessage (string from, DateTime date, bool incoming, string text, Habazie.kf2.Roster.Avatar a)
		{
			Gtk.TextIter iter = m_buffer.EndIter;
			m_buffer.InsertWithTags (ref iter, String.Format ("[{0}] ", date.ToShortTimeString ()), m_tagtime);
			m_buffer.InsertWithTags (ref iter, String.Format ("<{0}> ", from), incoming?m_tagin:m_tagout);
			m_buffer.Insert (ref iter, text);
			m_buffer.Insert (ref iter, "\n");
//			ScrollToEnd ();
		}
		

		public void DisplayEvent (string from, DateTime date, string text)
		{
			Gtk.TextIter iter = m_buffer.EndIter;
			m_buffer.InsertWithTags (ref iter, String.Format ("[{0}] ", date.ToShortTimeString ()), m_tagtime);
			m_buffer.InsertWithTags (ref iter, text, m_tagevent);
			m_buffer.Insert (ref iter, "\n");
//			ScrollToEnd ();
		}


		/// <summary>
		/// 	Scroll to the end of a view. 	
		/// </summary>
		public void ScrollToEnd ()
		{
			Gtk.TextIter iter = m_buffer.EndIter;
			m_textview.ScrollToIter (iter, 0.0, true, 0.0, 1.0);
		}
	}
}
